package com.gx.smart.building.ui.inspection.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ItemRegionLayoutBinding;
import com.huishi.gx.ib.grpc.lib.sysuser.sysregionapi.SysRegionDto;

/**
 * @author: xiaosy
 * 时间: 2022/6/15
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class RegionAdapter extends BaseAdapter<SysRegionDto, RegionAdapter.ViewHolder> {

    public int selectedPosition = -1;

    public RegionAdapter(Context context) {
        super(context);
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_region_layout;
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {
        ItemRegionLayoutBinding binding = ItemRegionLayoutBinding.inflate(LayoutInflater.from(mContext),
                (ViewGroup) view, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindData(ViewHolder holder, SysRegionDto item, int position) {
        holder.binding.name.setText(item.getName());
        if (selectedPosition == position) {
            holder.binding.name.setChecked(true);
        } else {
            holder.binding.name.setChecked(false);
        }
        holder.binding.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = position;
                notifyDataSetChanged();
            }
        });
        if (item.getChildrenCount() == 0) {
            holder.binding.arrow.setVisibility(View.INVISIBLE);
        } else {
            holder.binding.arrow.setVisibility(View.VISIBLE);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemRegionLayoutBinding binding;

        public ViewHolder(ItemRegionLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
